package control;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import pdf.PDFfile;
import server.mysqlConnection;

/**
 * 
 * @author Shay
 *	 *   This class responsible for the operation Search, when the client wants to activate this operation he inserts data in the GUI client
			,the data we insert into array string that will send to the server, In the server we maps the message by checking the first string (see AbstractControl).
*/

public class SearchControl extends AbstractControl
{

	public Object anlyze(String[] msg) {
		String[][] x=null;
		ArrayList<Object> result =new ArrayList<Object>();
		if(msg[0].contentEquals("Search"))
		{
			switch(msg[1])
			{
			case "truetruetruetrue":
				x=mysqlConnection.select("Select bid,name,author,location,(amount-borrowed),expectedreturndate from booktitle WHERE name like " + msg[2] + " AND author like " + msg[3] + " AND category like " +msg[4] + " AND description like " +msg[5],6);
				break;
			case "truefalsefalsefalse":
				x=mysqlConnection.select("Select bid,name,author,location,(amount-borrowed),expectedreturndate from booktitle WHERE name like " + msg[2],6);
				break;
			case "falsetruefalsefalse":
				x=mysqlConnection.select("Select bid,name,author,location,(amount-borrowed),expectedreturndate from booktitle WHERE author like " + msg[3],6);
				break;
			case "falsefalsetruefalse":
				x=mysqlConnection.select("Select bid,name,author,location,(amount-borrowed),expectedreturndate from booktitle WHERE category like " + msg[4],6);
				break;
			case "falsefalsefalsetrue":
				x=mysqlConnection.select("Select bid,name,author,location,(amount-borrowed),expectedreturndate from booktitle WHERE description like " + msg[5],6);
				break;
			case "truetruefalsefalse":
				x=mysqlConnection.select("Select bid,name,author,location,(amount-borrowed),expectedreturndate from booktitle WHERE name like " + msg[2] + " AND author like " + msg[3],6);
				break;
			case "truefalsetruefalse":
				x=mysqlConnection.select("Select bid,name,author,location,(amount-borrowed),expectedreturndate from booktitle WHERE name like " + msg[2] + " AND category like " + msg[4],6);
				break;
			case "truefalsefalsetrue":
				x=mysqlConnection.select("Select bid,name,author,location,(amount-borrowed),expectedreturndate from booktitle WHERE name like " + msg[2] + " AND description like " + msg[5],6);
				break;
			case "falsetruefalsetrue":
				x=mysqlConnection.select("Select bid,name,author,location,(amount-borrowed),expectedreturndate from booktitle WHERE author like " + msg[3] + " AND description like " + msg[5],6);
				break;
			case "falsetruetruefalse":
				x=mysqlConnection.select("Select bid,name,author,location,(amount-borrowed),expectedreturndate from booktitle WHERE author like " + msg[3] + " AND category like " + msg[4],6);
				break;
			case "falsefalsetruetrue":
				x=mysqlConnection.select("Select bid,name,author,location,(amount-borrowed),expectedreturndate from booktitle WHERE description like " + msg[5] + " AND category like " + msg[4],6);
				break;
			case "falsetruetruetrue":
				x=mysqlConnection.select("Select bid,name,author,location,(amount-borrowed),expectedreturndate from booktitle WHERE description like " + msg[5] + " AND category like " + msg[4] + " AND author like " + msg[3],6);
				break;
			case "truefalsetruetrue":
				x=mysqlConnection.select("Select bid,name,author,location,(amount-borrowed),expectedreturndate from booktitle WHERE description like " + msg[5] + " AND name like " + msg[2] + " AND author like " + msg[3],6);
				break;
			case "truetruefalsetrue":
				x=mysqlConnection.select("Select bid,name,author,location,(amount-borrowed),expectedreturndate from booktitle WHERE description like " + msg[5] + " AND name like " + msg[2] + " AND category like " + msg[4],6);
				break;
			case "truetruetruefalse":
				x=mysqlConnection.select("Select bid,name,author,location,(amount-borrowed),expectedreturndate from booktitle WHERE category like " + msg[3] + " AND name like " + msg[2] + " AND category like " + msg[4],6);
				break;
			}
			for(int i=0;i<x.length;i++)
			{
				if(Integer.parseInt(x[i][4])>0)
				{
					x[i][5]="N/A";
				}
			}
			result.add("Search");
			result.add(x);
			return result;
		}
		else if(msg[0].contentEquals("WatchIndex"))
		{
			try {
			result.add("WatchIndex");
			x=mysqlConnection.select("Select pathindex from booktitle where bid=" + msg[1], 1);
			if(x[0][0].contentEquals("unknown"))
			{
				result.add("unknown");
				return result;
			}
			File temp = new File(x[0][0]);
			PDFfile test=new PDFfile(temp);
			result.add(test);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return result;
		}
		result.add("FAIL");
		return result;
	}

	


	
	@Override
	public Boolean checkExist() {
		return null;
		// TODO Auto-generated method stub
		
	}

}
